#include "..\PakEngine\PakEngine.h"
#include <stdlib.h>

//#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")

void PlayerMovement(PakEngine *p, Sprite *player)
{
	if(p->Input.KeyIsPressed("up") && ( player->GetY() > 10 ) )
		player->Move(0,-1);

	if(p->Input.KeyIsPressed("down") && (  player->GetY() < 410 ) )
		player->Move(0,1);

	if(p->Input.KeyIsPressed("left") && (  player->GetX() > 10 ) )
		player->Move(-1,0);

	if(p->Input.KeyIsPressed("right") && ( player->GetX() < 310 ) )
		player->Move(1,0);
}

int main(void)
{
  PakEngine p("PakPong++",640,480);
  Sprite *bola	 = new Sprite(300,300,20,"Resources\\bola.png");
  Sprite *player = new Sprite(100,100,50,"Resources\\pala.png");
  Sprite *foe	 = new Sprite(400,200,50,"Resources\\pala.png");
  Sprite *campo  = new Sprite(0,0,100   ,"Resources\\campo.png");

  int xvel = 1, yvel = 1;
  int boing;

  boing = p.Sound.Load("Resources\\boing.wav");

 //Loop de engine
 while(p.Render())
 {
    PlayerMovement(&p,player);

	bola->Move(xvel,yvel);

	int mask = bola->IsNotInside(campo);

	if(mask)
		PAKENGINE_Play(boing);

	if(mask&PHYSICS_RIGHT)
	{
		bola->Move(-1,0);
		xvel = - (rand()%3 + 1);
		yvel += (rand()%3 -1);
	}

	if(mask&PHYSICS_LEFT)
	{
		bola->Move(1,0);
		xvel = (rand()%3 + 1);
		yvel += (rand()%3 -1);
	}

	if(mask&PHYSICS_UP)
	{
		bola->Move(0,1);
		xvel += (rand()%3 - 1);
		yvel = (rand()%3 +1);
	}

	if(mask&PHYSICS_DOWN)
	{
		bola->Move(0,-1);
		xvel += (rand()%3 - 1);
		yvel = - (rand()%3 + 1);
	}

	if(bola->GetX() > foe->GetX())
	{
		if(foe->GetX() < 630)
			foe->Move(1,0);
	}
	else
	{
		if(foe->GetX() > 320)
			foe->Move(-1,0);
	}
	
	if(bola->GetY() > foe->GetY())
	{
		if( foe->GetY() < 410 )
			foe->Move(0,1);
	}
	else
	{
		if ( foe->GetY() > 10 )
			foe->Move(0,-1);
	}

	if ( bola->CollisionWith(player)  || bola->CollisionWith(foe) )
	{
		xvel =- xvel;
		yvel += (rand()%3 -1);
	}

	p.Text.Draw(200,5,1,"PAK PONG");
	campo->Draw();
	player->Draw();
	foe->Draw();
	bola->Draw();
 }

 delete(&p);

 return 0;
}

/*
int main(void)
{
	Sprite *fondo;
	Sprite *nave[10] = {0}; 
	int i;

	PakEngine p("Pakteroids",1024,750);
	
	fondo = new Sprite();
	fondo->SetGraf("Resources\\estrellas.jpg");
	fondo->MoveTo(0,0,100);
	fondo->SetVisible(true);

	for( i = 0 ; i < 10 ; ++i)
	{
		nave[i] = new Sprite();
		nave[i]->SetGraf("Resources\\nave.png");
		nave[i]->SetVisible(true);
	}

	nave[0]->MoveTo(100,100,20);

	nave[1]->MoveTo(200,200,30);
	nave[1]->EscaleTo(0.0);

	nave[2]->MoveTo(300,500,40);
	nave[2]->EscaleTo(0.0);

	nave[3]->MoveTo(100,200,25);
	nave[3]->EscaleTo(45.0f); 

	nave[4]->MoveTo(200,550,25);
	nave[4]->EscaleTo(-45.0f);

	nave[5]->MoveTo(400,100,50);
	nave[5]->FlipX();

	nave[6]->MoveTo(500,650,50);
	nave[6]->FlipY();

	nave[7]->MoveTo(600,100,50);
	nave[7]->FlipXY();

	nave[8]->MoveTo(700,500,60);
	nave[8]->FlipY();
	
	nave[9]->MoveTo(800,600,70);
	nave[9]->FlipXY();
	nave[9]->EscaleTo(300.0f);

	while(p.Render())
	{
		fondo->Draw();

		for( i = 0 ; i < 10 ; ++i)
		{
			if(nave[i])
				nave[i]->Draw();
		}
		
		nave[0]->Rotate(0.1f);

		nave[1]->Escale(1.0f);

		nave[2]->Escale(0.5f);
		nave[2]->Rotate(-0.5f);

		nave[8]->Rotate(2.0f);

		nave[9]->Escale(-1.0f);
	}
}
*/
/*
int main(void)
{
	int fondo,nave;

	float angle = 0.0;

	PakEngine p("Pakteroids",1024,750);

	fondo = p.Graph.Load("Resources\\estrellas.jpg");

	nave = p.Graph.Load("Resources\\nave.png");

	while(p.Render())
	{
		p.Graph.Draw(0,0,100,fondo);
		p.Graph.DrawRotate(100,100,20,nave,angle);
		angle += 0.1;
	};

	delete(&p);

	return 0;
}
*/
/*
void PlayerMovement(PakEngine *p,int *x, int *y)
{
    if(p->Input.KeyIsPressed("up") && ( *y > 10 ) )
		(*y)--;

	if(p->Input.KeyIsPressed("down") && ( *y < 410 ) )
		(*y)++;

	if(p->Input.KeyIsPressed("left") && ( *x > 10 ) )
		(*x)--;

	if(p->Input.KeyIsPressed("right") && ( *x < 310 ) )
		(*x)++;
}

int main(void)
{
  int bola,pala,campo;
 	
  int x = 100,y= 100, xene = 400, yene= 200, xbola = 300, ybola = 300, xvel = 1, yvel = 1;
  int boing;

  PakEngine p("PakPong",640,480);

  bola = p.Graph.Load("Resources\\bola.png");
  pala = p.Graph.Load("Resources\\pala.png");
  campo = p.Graph.Load("Resources\\campo.png");

  boing = p.Sound.Load("Resources\\boing.wav");

 //Loop de engine
 while(p.Render())
 {
    PlayerMovement(&p, &x, &y);

	xbola += xvel;
	ybola += yvel;

	if ( p.Physics.NotInside(xbola,ybola,bola, 0,0,campo,"right" ) )
	{
		PAKENGINE_Play(boing);
		xbola--;
		xvel = - (rand()%3 + 1);
		yvel += (rand()%3 -1);
	}
	
	if ( p.Physics.NotInside(xbola,ybola,bola, 0,0,campo,"left" ) )
	{
		PAKENGINE_Play(boing);
		xbola++;
		xvel = (rand()%3 + 1);
		yvel += (rand()%3 -1);
	}

	if ( p.Physics.NotInside(xbola,ybola,bola, 0,0,campo,"up" ) )
	{
		PAKENGINE_Play(boing);
		ybola++;
		yvel = (rand()%3 + 1);
		xvel += (rand()%3 -1);
	}

	
	if ( p.Physics.NotInside(xbola,ybola,bola, 0,0,campo,"down" ) )
	{
		PAKENGINE_Play(boing);
		ybola--;
		yvel = - (rand()%3 + 1);
		xvel += (rand()%3 -1);
	}

	if(xbola > xene)
	{
		if(xene < 630)
			xene++;
	}
	else
	{
		if(xene > 320)
		xene--;
	}
	
	if(ybola > yene)
	{
		if( yene < 410 )
			yene++;
	}
	else
	{
		if ( yene > 10 )
			yene--;
	}

	if ( p.Physics.Collision(xbola,ybola,bola, x,y,pala ) || p.Physics.Collision(xbola,ybola,bola, xene,yene,pala ))
	{
		xvel =- xvel;
		yvel += (rand()%3 -1);
	}

	p.Text.Draw(200,5,1,"PAK PONG");
	p.Graph.Draw(0,0,100,campo);
	p.Graph.Draw(x,y,50,pala);
	p.Graph.Draw(xene,yene,50,pala);
	p.Graph.Draw(xbola,ybola,20,bola);
 }

 delete(&p);
 
 return 0;
}*/